import LocalNameEnum from "@/enumeration/LocalNameEnum";
import {getItemByDefault, setItem} from "@/utils/utools/DbStorageUtil";
import Constant from "@/global/Constant";
import MessageUtil from "@/utils/MessageUtil";
import {updateTo3ByWeb} from "@/components/version-manager/updateTo3";
import useLoadingStore from "@/store/LoadingStore";
import {statistics} from "@/global/BeanFactory";

export enum VersionStatus {
  // 新用户
  NEW = 1,
  // 版本更新
  UPDATE = 2,
  // 最新版本
  NONE = 3
}

const OLD_KEY: string = 'es-client.version';

export function versionManager(): VersionStatus {
  const oldVersion = getItemByDefault(OLD_KEY, '');
  let version = getItemByDefault(LocalNameEnum.KEY_VERSION, '');
  if (version == '') {
    version = oldVersion;
  }
  if (version === '') {
    setItem(LocalNameEnum.KEY_VERSION, Constant.version);
    MessageUtil.success("欢迎您使用es-client");
    updateTo();
    statistics.access("update", `新用户使用`);
    statistics.register();
    return VersionStatus.NEW;
  } else if (version != Constant.version) {
    setItem(LocalNameEnum.KEY_VERSION, Constant.version);
    MessageUtil.success("欢迎您更新到" + Constant.version);
    updateTo();
    statistics.access("update", `从${version}更新到${Constant.version}`);
    statistics.login();
    return VersionStatus.UPDATE;
  } else {
    return VersionStatus.NONE;
  }


}


function updateTo() {
  if (Constant.sign === 300) {
    updateTo3();
  }
}

export function updateTo3() {
  useLoadingStore().start("链接迁移")
  updateTo3ByWeb().then(() => MessageUtil.success("迁移完成"))
    .catch(e => MessageUtil.error("迁移失败", e))
    .finally(() => useLoadingStore().close());
}

